package com.ada.activiti;

import java.util.List;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.User;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstanceBuilder;
import org.activiti.engine.task.Task;

/**
 * Hello world!
 *
 */
public class App {
	public static void main(String[] args) {
		System.out.println("Hello World!");

		ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration
				.createStandaloneInMemProcessEngineConfiguration();
		processEngineConfiguration.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/activitidemo?characterEncoding=UTF-8");
		processEngineConfiguration.setJdbcDriver("com.mysql.jdbc.Driver");
		processEngineConfiguration.setJdbcUsername("root");
		processEngineConfiguration.setJdbcPassword("root");
		processEngineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
		ProcessEngine engine = processEngineConfiguration.buildProcessEngine();
		IdentityService identityService = engine.getIdentityService();
		User user = identityService.newUser("ada");
		user.setPassword("12344");
		List<User> users = identityService.createUserQuery().orderByUserId().desc().list();
		// for (User x : users) {
		// System.out.println(x.getId());
		// }
		// start(engine);
		// tasks(engine);
		// identityService.saveUser(user);
		List<ProcessDefinition> ps = engine.getRepositoryService().createProcessDefinitionQuery().list();
		for (ProcessDefinition processDefinition : ps) {
			System.out.println(processDefinition.getKey());
		}
	}

	private static void tasks(ProcessEngine engine) {
		TaskService taskService = engine.getTaskService();
		List<Task> tasks = taskService.createTaskQuery().taskAssignee("ada").list();
		for (Task task : tasks) {
			System.out.println(task.getAssignee());
			taskService.complete(task.getId());
			// taskService.claim(arg0, arg1);
		}
	}

	private static void start(ProcessEngine engine) {
		RuntimeService runtimeService = engine.getRuntimeService();
		ProcessInstanceBuilder builder = runtimeService.createProcessInstanceBuilder();
		builder.businessKey("myProcess");
		builder.processDefinitionId("myProcess:1:4");
		builder.addVariable("ada", "ada");
		builder.start();
	}

	private static void deploy(ProcessEngine engine) {
		RepositoryService repositoryService = engine.getRepositoryService();
		DeploymentBuilder builder = repositoryService.createDeployment();
		builder.addClasspathResource("MyProcess2.bpmn");
		builder.addClasspathResource("MyProcess2.png");
		builder.deploy();
	}
}
